package com.goldze.mvvmhabit.ui.new_ui;


import static com.goldze.mvvmhabit.utils.FileUtil.createImageFile;

import static me.goldze.mvvmhabit.utils.constant.ActionContants.REQUEST_TAKE_PHOTO;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.core.content.FileProvider;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.app.AppViewModelFactory;
import com.goldze.mvvmhabit.databinding.NewUiLayoutBinding;
import com.goldze.mvvmhabit.ui.main.DemoActivity;
import com.goldze.mvvmhabit.ui.network.NetWorkViewModel;
import com.google.gson.Gson;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class NewUiActivity extends BaseActivity<NewUiLayoutBinding, NewUiViewModel> {
    private String textString = "我是初始值";

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.new_ui_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        requestStoragePermissions();
        Intent bundle = getIntent();
        if (bundle != null) {
            textString = (String) bundle.getStringExtra("content");
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uploadEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                takePhoto();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.initData();
        viewModel.setTextContent(textString);
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("正在上传...");
        progressDialog.setCancelable(false);
        viewModel.getUploadProgress().observe(this, progress -> {
            KLog.i("*****进度***" + progress + "%");
            progressDialog.setMax((int) progress);
            progressDialog.setProgress((int) progress);
            if (progress >= 100) {
                progressDialog.dismiss();
            } else {
                progressDialog.show();
            }
        });


    }

    /**
     * 请求相机权限
     */
    private void requestStoragePermissions() {
        //请求打开文件操作权限
        RxPermissions rxPermissions = new RxPermissions(NewUiActivity.this);
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            ToastUtils.showShort("文件操作权限已经打开，直接操作");
                        } else {
                            ToastUtils.showShort("权限被拒绝");
                        }
                    }
                });
    }

    @Override
    public NewUiViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(NewUiViewModel.class);


    }

    public void takePhoto() {
//        Intent intent = new Intent(this, ImageGridActivity.class);
//        startActivityForResult(intent, REQUEST_TAKE_PHOTO);
        //如果直接想要打开相机
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
        startActivityForResult(intent, REQUEST_TAKE_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == REQUEST_TAKE_PHOTO) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                KLog.i("***获取到了照片地址***" + new Gson().toJson(images));
                viewModel.uploadFile(images.get(0).path);
                Glide.with(this).load(images.get(0).path).into(binding.image);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }

    }
}
